<?php
namespace jy_weishop_en;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class game extends \plugin{
	//time,参与时机'1'=>'订单完成时','2'=>'订单付款时',,type '1'=>'订单满额参与','2'=>'购买指定商品','3'=>'新用户参与'
	public function up_addgame($post=array(),$mid){
		$mid=$mid?:$_SESSION['uid'];
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>$post['type']);
		if($post['type']==1 && $post['price']){
			$params['price <=']=$post['price'];
		}
		if($post['time']){
			//$params['time']=$post['time'];
		}
		if($post['goodsid']){
			$params['goodsid']=$post['goodsid'];
		}
		$games=pdo_getall('jy_weishop_game',$params);
		if(count($games)){
			foreach($games as $game){
				if($game['type']==8){
					$game['pluginset']=json_decode($game['pluginset'],true);
					if($game['pluginset']['fenxiao_level']!=$post['level_id']){
						continue;		
					}
				}
				if($game){
						$indata=array(
							'mid'=>$mid,
							'game_id'=>$game['id'],
							'action'=>$game['action'],
							'plugin_name'=>$game['plugin_name'],
							'type'=>$game['type'],
							'remark'=>'购物参与活动',
						);
						$this->add_game($indata);
				}
			}
		}
		return true;
	}
	public function add_game($params,$url=''){
		$new_game_wait=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'game_id'=>$params['game_id'],
			'plugin_name'=>$params['plugin_name'],
			'action'=>$params['action'],
			'type'=>$params['type'],
			'status'=>0,
			'createtime'=>TIMESTAMP,
			'remark'=>$params['remark'],
		);
		pdo_insert('jy_weishop_game_wait',$new_game_wait);
		$id=pdo_insertid();
		$setting=$this->plugin_setting('setting');
		$url=$url?$url.'/id='.$id:'jy_weishop_en/game.give//id='.$id;
		$news=array(
			'title'=>'恭喜，您获得活动参与资格',
			'description'=>"快来参加吧，礼品多多哦！",
			'url'=>custom_url($url,true),
			'picurl'=>$setting['share_image'],
		);
		model('core/account')->send_news($news,$params['mid']);
		return true;
	}
	//检查游戏剩余次数
	public function check_num($gameid=0){
		$num=pdo_count('jy_weishop_game_wait',array('status'=>0,'game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		return $num;
	}
	//检查游戏权限
	public function check_game($id=0,$gameid=0){
		if($id){
			$wait=pdo_get('jy_weishop_game_wait',array('status'=>0,'id'=>$id,'mid'=>$_SESSION['uid']));
		}else{
			$wait=pdo_get('jy_weishop_game_wait',array('status'=>0,'game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		}
		if(!$wait['game_id']){
				exi('你暂无参与该游戏的名额！','error');
		}
		if($wait['game_id']){
			$wait['game_id']=pdo_getcolumn('jy_weishop_game',array('id'=>$wait['game_id']),'game_id');
			return true;
		}elseif($wait['plugin_name'] && $wait['plugin_name']!='core'){
			$gameinfo=$this->plugin($wait['plugin_name'])->get_active($id);
			return $gameinfo;
		}
		return false;
	}
	//检查游戏已参与次数
	public function canyu_num($gameid=0){
		$num=pdo_count('jy_weishop_game_getlog',array('game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		return $num;
	}
	//获取中奖LOG
	public function get_log($game_id=0,$page=1){
		$params=array('game_id'=>$game_id,'uniacid'=>$_SESSION['uniacid']);
		$pindex = max(1, intval($page));
		$psize = 10;
		$total='';
		$orderby='id desc';
		$parameter=array(
				'params'=>$params,
				'limit'=>array($pindex,$psize),
				'orderby'=>$orderby,
		);
		$logs=pdo_getlist('jy_weishop_game_getlog',$parameter,$total);
		return $logs;
	}
	//抽奖
	public function get_choujiang($game_id=0){
		if ($game_id) {
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$game['reward']=json_decode($game['reward'],true);
		}
		if($game['type']!=9 && $game['type']!=10){
			$check_num=$this->check_num($game_id);
			if($check_num<=0){
				exi('您已经没有抽奖次数了！','error');
			}
			$waitid=pdo_getcolumn('jy_weishop_game_wait',array('game_id'=>$game_id,'mid'=>$_SESSION['uid'],'status'=>0),'id');
		}elseif($game['type']==9){
			$myredfee=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']), 'redfee');
			if($myredfee<$game['delredfee']){
				exi('您当前红包余额不足！','error');
			}
		}elseif($game['type']==10){
			$daytime= mktime(0,0,0,date('m'),date('d'),date('Y'));
			if($game['delredtimes']){
				$istime=pdo_count('jy_weishop_game_getlog', array('createtime >'=>$daytime,'game_id'=>$game['id'],'type'=>10,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
				if($istime>=$game['delredtimes']){
					exi('今日积分抽奖次数已满！明天再来吧！','error');
				}
			}
			$credit1=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'credit1');
			if($credit1<$game['delredfee']){
				exi('您当前积分余额不足！','error');
			}
		}
		$reward=$game['reward'];
		$temreward = array();
		$nokucun=true;
		foreach ($reward as $key =>$value){
			$isgetnum=intval($value['isgetnum']);
			if ($value['type']==1){
				if ($value['num']>$isgetnum){
					$temreward[$key] = $value['rate'];
					if($value['rate']){
						$nokucun=false;
					}
				}
			}else if ($value['type']==7) {
				if ($value['num']>$isgetnum){
					$temreward[$key] = $value['rate'];
					if($value['rate']){
						$nokucun=false;
					}
				}
			}else{
				if ($value['num']>$isgetnum){
					$temreward[$key] = $value['rate'];
					if($value['rate']){
						$nokucun=false;
					}
				}
			}
		}
		if ($nokucun){
			exi('很遗憾,能中的奖品库存不足了!','error');
		}
		$reward_id = $this->getRand($temreward);
		$reward_info = $reward[$reward_id];
		$is_reward = 0;
		if (empty($reward_info)) {
			$is_reward = 0;
			$reward_info = '很遗憾,没有中奖';
			$reward_id=-1;
		}else{
			$is_reward = 1;	
		}
		$reward_info = $reward[$reward_id]['title'];
		$log_data = array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'game_id'=>$game['id'],
			'reward'=>ijson_encode($reward[$reward_id]),
			'rewardkey'=>$reward_id,
			'fee'=>$fee,
			'status'=>0,
			'type' => $game['type'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_game_getlog', $log_data);
		$logid=pdo_insertid();
		if($game['type']!=9 && $game['type']!=10){
			pdo_update('jy_weishop_game_wait',array('status'=>1),array('id'=>$waitid,'mid'=>$_SESSION['uid']));
		}elseif($game['type']==9){
			pdo_update('jy_weishop_member',array('redfee -='=>$game['delredfee']),array('mid'=>$_SESSION['uid']));
		}elseif($game['type']==10){
			model('core/member')->member_credit($_SESSION['uid'],'credit1',$game['delredfee'],false,'jy_weishop','参与大转盘抽奖');
		}
		$return = array(
			'status' =>1,
			'id' => $reward_id,
			'info' => $reward_info,
			'is_reward' => $is_reward,
			'logid' => $logid,
			'myredfee'=>$myredfee-$game['delredfee'],
			'credit1'=>$credit1-$game['delredfee'],
		);
		return $return;
	}
	//发放奖品
	public function get_gameres($id=0,$game_id=0){
		if($id){
			$wait=pdo_get('jy_weishop_game_wait',array('id'=>$id,'mid'=>$_SESSION['uid'],'status'=>0));
			$game=pdo_get('jy_weishop_game',array('id'=>$wait['game_id'],'uniacid'=>$_SESSION['uniacid']));
		}elseif($game_id){
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$wait=pdo_get('jy_weishop_game_wait',array('game_id'=>$game_id,'mid'=>$_SESSION['uid'],'status'=>0));
		}
		if($game['type']!=4 && (!$wait || !$game['id'])){
			exi('你已参与或游戏不存在！','error','jy_weishop_en/index/');
		}
		if($game['type']==4){
			$isfeelog=pdo_get('jy_weishop_game_getlog',array('game_id'=>$game['id'],'pin_id'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($isfeelog['id']){
				exi('你已参与该游戏！','error','jy_weishop_en/index/');
			}
		}
		if($game['plugin_name'] && $game['plugin_name']!='jy_h5game' && $game['plugin_name']!='jy_weishop'){
			$game['pluginset']=json_decode($game['pluginset'],true);
			$fee=$this->get_fee($game['id']);
			$status=$game['pluginset']['get_type']!=1?1:0;
			$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'game_id'=>$game['id'],
					'rewardtype'=>$game['pluginset']['rewardtype'],
					'fee'=>$fee,
					'status'=>$status,
					'createtime'=>TIMESTAMP
			);
			if($status){				
				pdo_insert('jy_weishop_game_getlog',$feelog);
			}else{
				//自己拼了一个
				pdo_insert('jy_weishop_game_feelog',$feelog);
				if($this->session('pin_id')){
					//助力一个
					$feelog['pin_id']=$this->session('pin_id');
					$feelog['fee']=$this->get_sharefee($game['id'],$feelog['pin_id']);
					pdo_insert('jy_weishop_game_feelog',$feelog);
				}
			}			
		}
		if($wait['id']){
			pdo_update('jy_weishop_game_wait',array('status'=>1),array('id'=>$wait['id']));
		}
		$return=array('error'=>0,'fee'=>$fee);
		return $return;
	}
	//领取奖品
	public function get_reward($game_id=0,$reward_id=0,$log_id=0){
		$mid=$mid?:$_SESSION['uid'];
		if (!empty($game_id)) {
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$game['reward']=json_decode($game['reward'],true);
		}
		$reward=$game['reward'];
		$reward=$reward[$reward_id];
		$fee=$this->set_reward($reward,$mid,$game,$log_id);
		$reward['isgetnum'] += 1;
		if ($reward['type']==1) {
		}elseif ($reward['type']==7) {
			$reward['isgetfee'] += $fee;
		}else{
			$reward['isgetfee'] += $reward['jifen'];
		}
		$temreward = $game['reward'];
		$temreward[$reward_id]=$reward;
		$updatedata = array('reward' => ijson_encode($temreward));
		$res=pdo_update('jy_weishop_game',$updatedata,array('uniacid' => $_SESSION['uniacid'],'id' =>$game_id));
		if ($res !== false) {
			$info = array(
				'status' => 1,
				'fee' => $fee,
				'info' => '恭喜您已获得《' . $temreward[$reward_id]['title'].'》'
			);
			return $info;
		}else{
			$info = array('status' => 0, 'info' => '获取奖励失败');
			return $info;
		}
	}
	public function set_reward($award,$mid,$game=array(),$log_id=0,$isexi=false)
	{
		$mid=$mid?:$_SESSION['uid'];
		if (empty($award) || empty($mid)) {
			return false;
		}
		//币奖励
		if($award['type']!=1 && $award['type']!=7){
			$awardstatus=2;
			$fee=$award['jifen']?:0;
		}elseif($award['type']==1){
			$awardstatus=1;
		}elseif($award['type']==7){
			$red_max=$award['rexmax']?:0;
			$red_min=$award['redmin']?:0;
			$fee=mt_rand($red_min*100,$red_max*100);
			$fee=$fee/100;
			$red=array(
				'mid'=>$mid,
				'money'=>$fee,
				'plugin'=>$game['plugin_name'],
				'remark'=>'参加活动，活动现金红包',
			);
			$awardstatus=2;
		}
		$game['pluginset']=json_decode($game['pluginset'],true);
		//推广下级数量
		if($game['pluginset']['sale_num']){
			$sale_num=pdo_count('core_members',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
			if($sale_num<$game['pluginset']['sale_num']){
				$lastnum=$game['pluginset']['sale_num']-$sale_num;
				if($isexi){
					exi('再邀请'.$lastnum.'人，才能领取奖励哦！','error');
				}else{
					return $fee;
				}
			}
		}
		if($award['type']!=1 && $award['type']!=7){
			$credittype=$award['type']==2?1:$award['type'];
			if($fee>0){
				if($game['merchid']){
					model('jy_merch/member')->addcreditlog($game['merchid'],'',$fee,$credittype,1);
				}else{
					cfc('core')->member_credit($mid,'credit'.$credittype,$fee,1,'jy_bigwheel','参与大转盘活动奖励');
				}
			}
		}elseif($award['type']==7){
			if($game['merchid']){
				//
			}else{
				cfc('red_pack')->get_money($red);
			}
		}
		pdo_update('jy_weishop_game_getlog',array('fee'=>$fee,'status'=>$awardstatus), array('id'=>$log_id));
		return $fee;
	}
	//获取参与奖励
	public function get_fee($gameid=0){
		$game=pdo_get('jy_weishop_game',array('id'=>$gameid,'uniacid'=>$_SESSION['uniacid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($game['pluginset']){
			$red_max=$game['pluginset']['red_max']?:0;
			$red_min=$game['pluginset']['red_min']?:0;
			$fee=mt_rand($red_min*100,$red_max*100);
			$fee=$fee/100;
		}
		return $fee;
	}
	//获取拉新奖励
	public function get_sharefee($gameid=0,$pin_id=0){
		$game=pdo_get('jy_weishop_game',array('id'=>$gameid,'uniacid'=>$_SESSION['uniacid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($game['pluginset']){
			$feelognum=pdo_count('jy_weishop_game_feelog',array('game_id'=>$gameid,'pin_id'=>$pin_id,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
			$red_shareset=$game['pluginset']['red_shareset']?:array();
			$nownum=0;
			if(count($red_shareset)){
				paixu($red_shareset,'num','desc');
				foreach($red_shareset as $rv){
					if($rv['num']>=$feelognum){
						$red_max=$rv['red_max']?:0;
						$red_min=$rv['red_min']?:0;
						$fee=mt_rand($red_min*100,$red_max*100);
						$fee=$fee/100;
					}
				}
			}			
		}
		return $fee;
	}
	private function getRand($proArr)
	{
		$result = '';
		$proSum = array_sum($proArr);
		foreach ($proArr as $key => $proCur) {
			$randNum = mt_rand(1, $proSum);
			if ($randNum <= $proCur) {
				$result = $key;
				break;
			}
			$proSum -= $proCur;
		}
		unset($proArr);
		return intval($result);
	}
}